package map.data
{
	import flash.geom.Point;
	
	import map.geometry.RadLine;

	public class RadCell
	{
		public var point1:Point;
		public var point2:Point;
		public var point3:Point;
		public var point4:Point;
		public var center:Point;
		public var neighbors:Array;
		
		public var surplusMobility:int;
		public var parent:RadCell;
		public var state:int;
		
		public function RadCell(point1:Point,point2:Point,point3:Point,point4:Point)
		{
			this.point1 = point1;
			this.point2 = point2;
			this.point3 = point3;
			this.point4 = point4;
			center = Point.interpolate(point1,point4,0.5);
		}
		
		public function isNeighbor(radcell:RadCell):Boolean
		{
			var points:Vector.<Point> = new Vector.<Point>();
			var points1:Vector.<Point> = new Vector.<Point>();
			points.push(point1);
			points.push(point2);
			points.push(point3);
			points.push(point4);
			
			points1.push(radcell.point1);
			points1.push(radcell.point2);
			points1.push(radcell.point3);
			points1.push(radcell.point4);
			var result:int = 0;
			for each(var point:Point in points)
			{
				for each(var point1:Point in points1)
				{
					if(Point.distance(point1,point) < 0.1)
					{
						result ++;
						break;
					}
				}
			}
			return result == 2;
		}
		
		public function containPoint(target:Point):Boolean
		{
			var isMinx:Boolean = true;
			var isMiny:Boolean = true;
			var isMaxx:Boolean = true;
			var isMaxy:Boolean = true;
			var points:Vector.<Point> = new Vector.<Point>();
			points.push(point1);
			points.push(point2);
			points.push(point3);
			points.push(point4);
			for each(var point:Point in points)
			{
				if(point.x < target.x)
				{
					isMinx = false;
				}
				if(point.x > target.x)
				{
					isMaxx = false;
				}
				if(point.y < target.y)
				{
					isMiny = false;
				}
				if(point.y > target.y)
				{
					isMaxy = false;
				}
			}
			return !isMinx && !isMiny && !isMaxx && !isMaxy;
		}
		
		public function positionEqual(target:RadCell):Boolean
		{
			return Point.distance(target.center,this.center)<1;
		}
		
		/**
		 * 
		 * @return 与目标格子之间的3d实际距离，单位为3d空间单位格子宽度 
		 * 
		 */		
		public function Distance_3d(target:RadCell):Number
		{
			var rowAndCol:Array = rowAndCol(target);
			return Math.sqrt(Math.pow(rowAndCol[0],2)+Math.pow(rowAndCol[1],2));
		}
		
		public function rowAndCol(target:RadCell):Array
		{
			var rline:RadLine = new RadLine(point1,point2);
			var cline:RadLine = new RadLine(point1,point3);
			var row:int = Math.round(rline.pointDistance(target.center)/rline.pointDistance(center))/2;
			var col:int = Math.round(cline.pointDistance(target.center)/rline.pointDistance(center))/2;
			return [row,col];
		}
		
		public function clone():RadCell
		{
			var result:RadCell = new RadCell(this.point1,this.point2,this.point3,this.point4);
			result.surplusMobility = this.surplusMobility;
			result.parent = this.parent;
			result.state = this.state;
			return result;
		}
		
		public static function decode(obj:Object):RadCell
		{
			var p1:Point = new Point(obj.point1.x,obj.point1.y);
			var p2:Point = new Point(obj.point2.x,obj.point2.y);
			var p3:Point = new Point(obj.point3.x,obj.point3.y);
			var p4:Point = new Point(obj.point4.x,obj.point4.y);
			var result:RadCell = new RadCell(p1,p2,p3,p4);
			result.neighbors = obj.neighbors
			return result;
		}
	}
}